import {
  IRegisterMoudle,
  IADQContaniner,
  IGlobalConfig,
  ICache,
} from "../Interfaces/IIOC";
import {
  IGameStartup,
  ISceneManager,
  IGameEnvironment,
  IProgressManager,
  ISoundManager,
  IScreenAdaptive,
  ILocalStorage,
} from "../Interfaces/IGameManager";
import { IDataFactory } from "../Interfaces/IDataTable";
import { IHttpHelper } from "../Interfaces/INetwork";
import {
  IEventHandler,
  IDataChangeTool,
  IDictionary,
} from "../Interfaces/IUtils";
import IDialog from "../Interfaces/IDialog";

/**游戏管理器 */
export default class ADQ {
  /**全局数据编号名称 */
  public static IdName: string = "id";
  /**加载容器数据 */
  public static setContaniner() {
    this.globalSound = this.getResolve<ISoundManager>("ISoundManager");
  }
  public static container: IADQContaniner;
  /**初始全局容器 */
  public static getContaniner(): IADQContaniner {
    return this.container;
  }
  /**
   * 从容器解析接口
   * @param ifName 接口名称
   */
  public static getResolve<T>(ifName): T {
    return this.container.Resolve<T>(ifName);
  }
  /**系统环境信息 */
  public static get GameEnvironment(): IGameEnvironment {
    return this.getResolve<IGameEnvironment>("IGameEnvironment");
  }
  /**获取字典类 */
  public static getDictionary<K, V>(): IDictionary<K, V> {
    return this.getResolve<IDictionary<K, V>>("IDictionary");
  }
  /**
   * 获取游戏启动实例
   */
  public static getGameStartup(): IGameStartup {
    return this.getResolve<IGameStartup>("IGameStartup");
  }
  /**
   * 获取游戏启动实例
   */
  public static getDialog(): IDialog {
    return this.getResolve<IDialog>("IDialog");
  }
  /**
   * 获取新游戏音效处理对象
   */
  public static getSound(): ISoundManager {
    return this.getResolve<ISoundManager>("ISoundManager");
  }
  /**
   * 获取全局游戏音效处理
   */
  public static globalSound: ISoundManager;
  /**
   * 获取游戏启动实例
   */
  public static get globalConfig(): IGlobalConfig {
    return this.getResolve<IGlobalConfig>("IGlobalConfig");
  }
  /**获取缓存数据-ADQ.Cache[DataType.] */
  public static get Cache(): ICache {
    return this.getResolve<ICache>("ICache");
  }

  /**游戏适配 */
  public static get adaptive(): IScreenAdaptive {
    return this.getResolve<IScreenAdaptive>("IScreenAdaptive");
  }

  /**
   * 获取数据转换接口
   */
  public static get dataChangeTool(): IDataChangeTool {
    return this.getResolve<IDataChangeTool>("IDataChangeTool");
  }

  /**
   * 获取游戏数据工厂实例
   */
  public static getDataFactory(): IDataFactory;
  /**根据标识获取数据集合 */
  public static getDataFactory<T>(tag: number): Array<T>;
  public static getDataFactory<T>(tag: number, id: number): T;
  public static getDataFactory(tag?: number, id?: number): any {
    if (tag == null) {
      return this.getResolve<IDataFactory>("IDataFactory");
    } else {
      if (id == null)
        return this.getResolve<IDataFactory>("IDataFactory").getDataList(tag);
      else
        return this.getResolve<IDataFactory>("IDataFactory").getDataByID(
          tag,
          id
        );
    }
  }

  /**
   * 获取HTTP网络通信接口
   */
  public static get Http(): IHttpHelper {
    return this.getResolve<IHttpHelper>("IHttpHelper");
  }
  /**本地存储接口 */
  public static get storage(): ILocalStorage {
    return this.getResolve<ILocalStorage>("ILocalStorage");
  }

  /**
   * 获取资源统计接口
   */
  public static getProgressManager(
    compeletFn: IEventHandler,
    imaxs: number
  ): IProgressManager {
    var resolve = this.container.ResolveToFunc("IProgressManager");
    return new resolve.prototype.constructor(compeletFn, imaxs);
  }
  /**
   * 获取场景管理接口
   */
  public static getSceneManager(): ISceneManager {
    return this.getResolve<ISceneManager>("ISceneManager");
  }
  /**获取事件处理接口 */
  public static getHandler(): IEventHandler;
  /**
   * 获取作用域指向事件处理
   * @param caller 作用域
   * @param func 执行函数
   * @param once 是否只执行一次，默认true
   */
  public static getHandler(caller, func, once?: boolean): IEventHandler;
  public static getHandler(caller?, func?, once?: boolean): IEventHandler {
    if (caller)
      return this.getResolve<IEventHandler>("IEventHandler").create(
        caller,
        func,
        null,
        once
      );
    else return this.getResolve<IEventHandler>("IEventHandler");
  }
  /**
   * 执行指定作用域 一次性函数
   * TIPS:传入指向参数时，立即执行事件，且无返回值
   * @param caller 作用域（this）
   * @param func 执行函数
   * @param args 传入参数-说明：[1,2]
   */
  public static runHandler(caller, func, args?: any[]) {
    this.getHandler().create(caller, func, args).run();
  }
  /**全局进度加载器 */
  static progressManager: IProgressManager;
}
